<template>
  <div class="profile-container">
    <div class="header">
      <h1>我的</h1>
    </div>
    <div class="user-info">
      <img class="user-avatar" :src="user.img" alt="用户头像" />
      <div class="user-details">
        <p class="user-name">{{user.userName}}</p>
        <p class="user-id">ID:{{user.userId}}</p>
        <p class="user-phone">电话:{{user.phone}}</p>
      </div>
    </div>
    <div class="game-section">
      <div class="game-card" @click="goToGames">
        <p>我的游戏</p>
        <button class="game-button" @click="zpw">找陪玩</button>
      </div>
      <div class="game-card" @click="goToGameSettings">
        <p>王者荣耀</p>
      </div>
      <div class="game-card" @click="goToOtherGame">
        <p>英雄联盟</p>
      </div>
    </div>
    <div class="commission-info" v-show="user.pid">
      <p>我的佣金(元)</p>
      <h1>{{a.balance}}</h1>
      <button @click="tixian">去提现</button>
    </div>
    <div class="options-list">
      <div class="option-item" @click="requestToBePlayer" v-show="user.pid==null">
        <span>申请成为陪玩人员</span>
        <img class="arrow-icon" src="arrow-icon.png" alt="箭头" />
      </div>
      <div class="option-item" @click="requestToG()" v-show="user.pid">
        <span>个人信息</span>
        <img class="arrow-icon" src="arrow-icon.png" alt="箭头" />
      </div>
      <div class="option-item" @click="goToMyOrders">
        <span>我的订单</span>
        <img class="arrow-icon" src="arrow-icon.png" alt="箭头" />
      </div>
      <div class="option-item" @click="logout">
        <span>退出登录</span>
        <img class="arrow-icon" src="arrow-icon.png" alt="箭头" />
      </div>
    </div>
    <van-tabbar route>
      <van-tabbar-item replace to="/home" icon="home-o">首页</van-tabbar-item>
      <van-tabbar-item replace to="/NotificationList" icon="setting-o">消息</van-tabbar-item>
      <van-tabbar-item replace to="/mine" icon="friends-o">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      user: {},
      player:{},
      a:{}
    };
  },
  methods: {
    requestToG(){
      this.$router.push({path: '/personalDetails', query: {user: JSON.stringify(this.user)}});
    },
 zpw() {
      this.$router.push('/king'); // 跳转到找陪玩页面
    }, tixian(){
      this.$router.push({path: '/payouts', query: {a: JSON.stringify(this.a)}});
    },    goToGames() {
      this.$router.push('/my-games'); // 跳转到我的游戏页面
    },
    servePlayer() {
      axios.post('http://localhost:8083/play/list', this.player).then(response => {
        this.player = response.data.data;
        const a = this.player.filter(item => item.id == this.user.pid);
        console.log(this.player)
        this.a=a[0];
        console.log(a)
      }).catch(error => {
        console.error('Error fetching player:', error);
        this.error = error;
      })
    },
    getUserDetail(){
      axios.get('/user/user/getUserById/'+this.user.id).then(res => {
        this.user = res.data;
        console.log(res.data);
        this.servePlayer();
      });
    },
    goToGameSettings() {
      this.$router.push('/king-glory'); // 跳转到王者荣耀页面
    },
    goToOtherGame() {
      this.$router.push('/league-of-legends'); // 跳转到英雄联盟页面
    },
    requestToBePlayer() {
      this.$router.push('/applyfor'); // 跳转到申请成为陪玩页面
    },
    goToMyOrders() {
      this.$router.push('/indent'); // 跳转到我的订单页面
    },
    logout() {
      // 清除 localStorage 和 sessionStorage 中的用户信息
      localStorage.removeItem('user');
      sessionStorage.removeItem('id');

      // 提示用户已经退出
      this.$message.success('退出成功');

      // 跳转至登录页面
      this.$router.push('/login');
    },
  },
  created() {
    this.user.id=sessionStorage.getItem('id');
    this.getUserDetail();


  },
};
</script>

<style scoped>
.profile-container {
  padding: 20px;
  background-color: #f7f9fc;
  height: 100vh;
}

.header {
  text-align: center;
}

.user-info {
  display: flex;
  align-items: center;
  margin: 20px 0;
}

.user-avatar {
  width: 70px;
  height: 70px;
  border-radius: 35px;
  margin-right: 15px;
}

.user-details {
  flex-grow: 1;
}

.user-name {
  font-size: 1.5em;
  font-weight: bold;
}

.user-id,
.user-phone {
  color: #666;
  font-size: 0.9em;
}

.game-section {
  display: flex;
  justify-content: space-between;
  margin: 20px 0;
}

.game-card {
  flex-grow: 1;
  background-color: #e1f5fe;
  padding: 15px;
  border-radius: 8px;
  margin: 0 10px;
  text-align: center;
  cursor: pointer;
}

.game-card .game-button {
  margin-top: 10px;
  background-color: #00aaff;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 5px 10px;
  cursor: pointer;
}

.options-list {
  margin-top: 20px;
}

.option-item {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border-bottom: 1px solid #eaeaea;
  cursor: pointer;
}

.arrow-icon {
  width: 12px;
  height: 12px;
}

.commission-box {
  width: 300px;
  background-color: #f0f8ff;
  border-radius: 10px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.user-info {
  text-align: center;
  margin-bottom: 20px;
}

.user-info img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
}

.commission-info {
  text-align: center;
}

.commission-info h1 {
  font-size: 24px;
  margin: 10px 0;
}

.commission-info button {
  background-color: #007BFF;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
}
</style>
